package com.mdxx.patrol.system.service.impl;


import com.mdxx.patrol.system.param.PermissionQueryListParam;
import com.mdxx.patrol.system.param.PermissionSaveParam;
import com.mdxx.patrol.system.param.PermissionToDelParam;
import com.mdxx.patrol.system.service.IPermissionService;
import com.mdxx.patrol.system.vo.PermissionQueryForTreeVo;
import com.mdxx.patrol.system.vo.PermissionQueryListVo;
import com.mdxx.common.CommonResult;
import com.mdxx.common.ResultFactory;
import com.mdxx.common.TableData;
import com.mdxx.patrol.system.bean.SysPermission;
import com.mdxx.patrol.system.bean.SysRole;
import com.mdxx.patrol.system.dto.PermissionDto;
import com.mdxx.patrol.system.mapper.SysPermissionMapper;
import com.mdxx.patrol.system.mapper.SysRoleMapper;
import org.apache.commons.lang.time.FastDateFormat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by MD-01 on 2017/5/17.
 */
@Service
@Transactional
public class PermissionImpl implements IPermissionService {

    @Resource
    private SysPermissionMapper permissionMapper;
    @Resource
    private SysRoleMapper roleMapper;

    @Override
    public CommonResult<TableData<PermissionQueryListVo>> queryList(PermissionQueryListParam param) {

        List<PermissionDto> permissions = permissionMapper.queryList(param.getStart(),param.getNum());
        List<PermissionQueryListVo> vos = new ArrayList<>();
        FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
        for(PermissionDto p : permissions){
            PermissionQueryListVo vo = new PermissionQueryListVo();
            vo.setCreateTime(format.format(p.getCreateTime()));
            vo.setName(p.getName());
            vo.setOrderNum(p.getOrderNum());
            vo.setParentId(p.getParentId());
            vo.setPermissionId(p.getPermissionId());
            vo.setType(p.getType());
            vo.setUrl(p.getUrl());
            vo.setParentName(p.getParentName());
            vo.setTag(p.getTag());
            vo.setUsable(p.getUsable());
            vos.add(vo);
        }

        int total = permissionMapper.queryListCount();
        TableData<PermissionQueryListVo> data = new TableData<>();
        data.setData(vos);
        data.setRecordsTotal(total);
        data.setRecordsFiltered(total);
        return ResultFactory.ok(data);
    }

    @Override
    public CommonResult<Void> save(PermissionSaveParam param) {

        //新增
        if(param.getPermissionId()==null){
            SysPermission permission = permissionMapper.queryByTag(param.getTag());
            if(permission!=null){
                return ResultFactory.error("该标识已经存在");
            }
            permission = new SysPermission();
            permission.setTag(param.getTag());
            permission.setUsable(param.getUsable());
            permission.setUrl(param.getUrl());
            permission.setType(param.getType());
            permission.setParentId(param.getParentId());
            permission.setOrderNum(param.getOrderNum());
            permission.setName(param.getName());
            permission.setCreateTime(new Date());
            permissionMapper.insert(permission);
            return ResultFactory.ok();
        }

        SysPermission permission = permissionMapper.selectById(param.getPermissionId());
        if(permission==null){
            return ResultFactory.error("错误的权限编号");
        }
        permission.setTag(param.getTag());
        permission.setUsable(param.getUsable());
        permission.setUrl(param.getUrl());
        permission.setType(param.getType());
        permission.setParentId(param.getParentId());
        permission.setOrderNum(param.getOrderNum());
        permission.setName(param.getName());
        permissionMapper.updateById(permission);

        return ResultFactory.ok();
    }

    @Override
    public CommonResult<Void> toDel(PermissionToDelParam param) {
        SysPermission permission = permissionMapper.selectById(param.getPermissionId());
        if(permission==null){
            return ResultFactory.error("错误的权限编号");
        }

        List<SysPermission> permissions = permissionMapper.queryByParentId(permission.getParentId());
        if(permissions.size()>0){
            return ResultFactory.error("该权限有子权限.不能删除");
        }

        //判断是否有角色有该权限
        List<SysRole> roleList = roleMapper.queryByPermissionId(param.getPermissionId());
        if(roleList.size()>0){
            return ResultFactory.error("该权限已经被绑定到角色上了,不能删除");
        }
        permissionMapper.deleteById(param.getPermissionId());
        return ResultFactory.ok();
    }


    /**
     * 获取所有权限生成权限树
     * @return
     */
    public CommonResult<List<PermissionQueryForTreeVo>> queryForTree(){

        List<SysPermission> permissions = permissionMapper.queryAllUsable();

        List<PermissionQueryForTreeVo> vos = new ArrayList<>();
        for(SysPermission p : permissions){
            PermissionQueryForTreeVo vo = new PermissionQueryForTreeVo();
            vo.setName(p.getName());
            vo.setParentId(p.getParentId());
            vo.setPermissionId(p.getId());
            vo.setType(p.getType());
            vo.setUsable(p.getUsable());
            vos.add(vo);
        }
        return ResultFactory.ok(vos);
    }
}
